<?php

namespace app\admin\model;

use think\Model;

class PayslipFeedback extends Model
{
	// 表名
	protected $name = 'payslip_feedback';

	// 自动写入时间戳字段
	protected $autoWriteTimestamp = 'integer';

	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = FALSE;

	// 追加属性
	protected $append = [
		'status_text',
	];

	public function getStatusList()
	{
		return ['1' => '未处理', '2' => '已处理'];
	}

	public function getStatusTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
		$list  = $this->getStatusList();
		return isset($list[$value]) ? $list[$value] : '';
	}

	public static function feedback_log($org_logs, $content, $author = 'Q', $key = -1)
	{
		$feedback_log = is_array($org_logs) ? $org_logs : json_decode($org_logs, TRUE);
		if ($author == 'Q') { //新的问题
			$feedback_log[] = [
				'question'      => $content,
				'question_time' => datetime(time()),
				'answer'        => '',
				'answer_time'   => ''
			];
		} elseif ($key >= 0 && isset($feedback_log[$key])) { //回复，需要问题对应的key
			$feedback_log[$key]['answer']      = $content;
			$feedback_log[$key]['answer_time'] = datetime(time());
		}
		return json_encode($feedback_log, JSON_UNESCAPED_UNICODE);
	}

	public function user()
	{
		return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
}
